package com.linjiaxin.controller;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.propertyeditors.PropertiesEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class BaseController {
		
		/**
		 * 
		    * @Title: initBinder
		    * @Description: TODO(请求参数绑定)
		    * @param @param binder    参数
		    * @return void    返回类型
		    * @throws
		 */
		@InitBinder    
	   protected void initBinder2(WebDataBinder binder) {
			// String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
			binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
				@Override
				public void setAsText(String text) {
					setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
				}
				@Override
				public String getAsText() {
					Object value = getValue();
					return value != null ? value.toString() : "";
				}
			});
	      /* 
	       //springmvc自带的类型转换类
	       binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
	       binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));    
	       binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));  
	       binder.registerCustomEditor(double.class, new CustomNumberEditor(double.class, true));  
	       binder.registerCustomEditor(float.class, new CustomNumberEditor(float.class, true));  */

	       
	       //自定义类型转换类
	       binder.registerCustomEditor(Date.class, new MyDateEditor());    
	       binder.registerCustomEditor(int.class, new IntegerEditor());
	       binder.registerCustomEditor(long.class, new LongEditor());
	       binder.registerCustomEditor(double.class, new DoubleEditor());
	       binder.registerCustomEditor(float.class, new FloatEditor());
	   }

		
		private class MyDateEditor extends PropertiesEditor {
	        @Override
	        public void setAsText(String text) throws IllegalArgumentException {
	            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	            Date date = null;
	            try {
	                date = format.parse(text);
	            } catch (ParseException e) {
	                format = new SimpleDateFormat("yyyy-MM-dd");
	               
	                    try {
							date = format.parse(text);
						} catch (ParseException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
	            }
	            setValue(date);
	        }
	    }
		
		public class DoubleEditor extends PropertiesEditor {    
		    @Override    
		    public void setAsText(String text) throws IllegalArgumentException {    
		        if (text == null || text.equals("")) {    
		            text = "0";    
		        }    
		        setValue(Double.parseDouble(text));    
		    }    
		    
		    @Override    
		    public String getAsText() {    
		        return getValue().toString();    
		    }    
		}
		
		
		public class IntegerEditor extends PropertiesEditor {    
		    @Override    
		    public void setAsText(String text) throws IllegalArgumentException {    
		        if (text == null || text.equals("")) {    
		            text = "0";    
		        }    
		        setValue(Integer.parseInt(text));    
		    }    
		    
		    @Override    
		    public String getAsText() {    
		        return getValue().toString();    
		    }    
		}  
		
		
		public class FloatEditor extends PropertiesEditor {    
		    @Override    
		    public void setAsText(String text) throws IllegalArgumentException {    
		        if (text == null || text.equals("")) {    
		            text = "0";    
		        }    
		        setValue(Float.parseFloat(text));    
		    }    
		    
		    @Override    
		    public String getAsText() {    
		        return getValue().toString();    
		    }    
		}   
		
		
		public class LongEditor extends PropertiesEditor {    
		    @Override    
		    public void setAsText(String text) throws IllegalArgumentException {    
		        if (text == null || text.equals("")) {    
		            text = "0";    
		        }    
		        setValue(Long.parseLong(text));    
		    }    
		    
		    @Override    
		    public String getAsText() {    
		        return getValue().toString();    
		    }    
		}    
}
